package com.unittec.drainage.provider.auth.excel.imports;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.stereotype.Service;

import com.unittec.drainage.provider.common.ServiceIdConstants;
import com.unittec.drainage.provider.entity.ImportTaskEntity;
import com.unittec.drainage.provider.log.Log;
import com.unittec.drainage.provider.scope.ThreadLocalScope;
import com.unittec.drainage.provider.service.HystrixRestTemplateService;
import com.unittec.drainage.provider.utils.CopyUtils;
import com.unittec.drainage.sdk.auth.domain.ImportTaskConfig;
import com.unittec.drainage.sdk.auth.request.Req101904;
import com.unittec.drainage.sdk.auth.request.Req101905;
import com.unittec.drainage.sdk.auth.request.Req101906;
import com.unittec.drainage.sdk.auth.response.Res101904;
import com.unittec.drainage.sdk.auth.response.Res101905;
import com.unittec.drainage.sdk.auth.response.Res101906;
import com.unittec.drainage.sdk.root.domain.GatewayRequest;
import com.unittec.drainage.sdk.root.domain.Result;
import com.unittec.drainage.sdk.root.enums.BizCode;
import com.unittec.drainage.sdk.root.internal.exception.BusinessException;

;

/**
 * @Description:
 * @Author: wuxinwei
 * @CreateDate: 2019/11/15 16:17
 */
@Service
public class ImportTaskService {

    @Autowired
    private HystrixRestTemplateService hystrixRestTemplateService;

    @Autowired
    private Log log;

    public void save(ImportTaskEntity entity) {
        log.sysInfoLog("save ", "begin");
        try {
            GatewayRequest<Req101904> getWay = new GatewayRequest<>();
            Req101904 req = new Req101904();
            ImportTaskConfig importTaskConfig = new ImportTaskConfig();
            CopyUtils.copyProperties(entity, importTaskConfig);
            req.setImportTaskConfig(importTaskConfig);
            getWay.setContent(req);
            getWay.setMethod("101904");
            Result<Res101904> result = hystrixRestTemplateService.exchange(ServiceIdConstants.AUTH_101904, getWay,
                ThreadLocalScope.getAuthData(), new ParameterizedTypeReference<Result<Res101904>>() {});
            if (!result.isSuccess()) {
                log.error(getWay, BizCode.SAVE_ERROR.getCode(), "新增任务失败");
                throw new BusinessException("新增任务失败");
            }
        } catch (BeansException e) {
            log.sysErrLog("save", "新增任务失败", e);
            throw new BusinessException("新增任务失败");
        }

        log.sysInfoLog("save", "end");
    }

    public void updateById(ImportTaskEntity entity) {
        log.sysInfoLog("updateById ", "begin");
        try {
            GatewayRequest<Req101905> getWay = new GatewayRequest<>();
            Req101905 req = new Req101905();
            ImportTaskConfig importTaskConfig = new ImportTaskConfig();
            CopyUtils.copyProperties(entity, importTaskConfig);
            req.setImportTaskConfig(importTaskConfig);
            getWay.setContent(req);
            getWay.setMethod("101905");
            Result<Res101905> result = hystrixRestTemplateService.exchange(ServiceIdConstants.AUTH_101905, getWay,
                ThreadLocalScope.getAuthData(), new ParameterizedTypeReference<Result<Res101905>>() {});
            if (!result.isSuccess()) {
                log.error(getWay, BizCode.SAVE_ERROR.getCode(), "修改任务失败");
                throw new BusinessException("修改任务失败");
            }

        } catch (BeansException e) {
            log.sysErrLog("updateById", "修改任务失败", e);
            throw new BusinessException("修改任务失败");
        }
        log.sysInfoLog("save", "end");
    }

    public ImportTaskEntity getById(String id) {
        log.sysInfoLog("getById ", "begin");
        ImportTaskEntity importTaskEntity = new ImportTaskEntity();
        try {
            GatewayRequest<Req101906> getWay = new GatewayRequest<>();
            Req101906 req = new Req101906();
            req.setId(id);
            getWay.setContent(req);
            getWay.setMethod("101906");
            Result<Res101906> result = hystrixRestTemplateService.exchange(ServiceIdConstants.AUTH_101906, getWay,
                ThreadLocalScope.getAuthData(), new ParameterizedTypeReference<Result<Res101906>>() {});

            if (result.isSuccess()) {
                if (result.getData() != null && result.getData().getImportTaskConfig() != null) {
                    ImportTaskConfig entity = result.getData().getImportTaskConfig();
                    CopyUtils.copyProperties(entity, importTaskEntity);
                } else {
                    log.error(getWay, BizCode.QUERY_ERROR.getCode(), "获取任务详情失败");
                    throw new BusinessException("获取任务详情失败");
                }
            } else {
                log.error(getWay, BizCode.QUERY_ERROR.getCode(), "获取任务详情失败");
                throw new BusinessException("获取任务详情失败");
            }

        } catch (BeansException e) {
            log.sysErrLog("getById", "修改任务失败", e);
            throw new BusinessException("修改任务失败");
        }
        log.sysInfoLog("getById", "end");
        return importTaskEntity;
    }
}
